package com.cm.sys.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.cm.sys.framework.aop.NotCheckIP;
import com.cm.sys.framework.aop.NotCheckLogin;
import com.cm.sys.framework.application.RequestParameter;
import com.cm.sys.framework.application.ResponseResult;
import com.cm.sys.framework.utils.JsonUtil;

import com.cm.sys.framework.utils.JwtUtils;
import com.cm.sys.system.entity.Menu;
import com.cm.sys.system.entity.User;
import com.cm.sys.system.service.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassNameMenuController
 * @Description
 * @Author WZC
 * @Date 2019/11/30 6:08
 * @Version V1.0
 **/
@Controller
@RequestMapping(value = "/menu")
public class MenuController {
    private static final Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired(required = false)
    private MenuService menuService;

    @GetMapping(value = "/activityTypeMenu")  
    public ResponseResult activityTypeMenu(RequestParameter rp) {
        ResponseResult responseResult = new ResponseResult();
        String token = rp.getToken();
        User user = JwtUtils.validateToken(token);

        // 获取用户登录时获得的 Token
 
        Map<String, Object> useDynamicMenus= menuService.activityTypeMenuGroup(user.getId());
        responseResult.setMap(useDynamicMenus);
        return responseResult;
    }



    /***
     * 新增菜单
     * ===================================================================================================
     * @param rp
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST) 
    public ResponseResult postMenu(RequestParameter rp) {
        JSONObject args = rp.getparameters();
        logger.info("菜单保存的入参==================【{}】===============", args);
        ResponseResult responseResult = new ResponseResult();
        Integer parentId = JsonUtil.getInt(args, "parentId", 0);
        Integer sortInt = JsonUtil.getInt(args, "sortInt");
        Integer isShow = JsonUtil.getInt(args, "isShow");
        String parentName = JsonUtil.getString(args, "parentName");
        String route = JsonUtil.getString(args, "route");
        String icon = JsonUtil.getString(args, "icon");
        Menu menu = new Menu();
        menu.setParentId(parentId);
        menu.setSortInt(sortInt);
        menu.setParentName(parentName);
        menu.setRoute(route);
        menu.setIcon(icon);
        menu.setIsShow(isShow);
        menuService.saveMenu(menu);
        return responseResult;
    }


    /***
     * 更新菜单
     * @param rp
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @NotCheckLogin
    @NotCheckIP
    public ResponseResult putMenu(RequestParameter rp, @PathVariable("id") String id) {
        ResponseResult responseResult = new ResponseResult();
        JSONObject args = rp.getparameters();
        logger.info("菜单修改的入参==================【{}】===============", args);
        Integer parentId = JsonUtil.getInt(args, "parentId", 0);
        Integer sortInt = JsonUtil.getInt(args, "sortInt");
        Integer isShow = JsonUtil.getInt(args, "isShow");
        String parentName = JsonUtil.getString(args, "parentName");
        String route = JsonUtil.getString(args, "route");
        String icon = JsonUtil.getString(args, "icon");
        Menu menu = new Menu();
        menu.setId(Integer.parseInt(id));
        menu.setParentId(parentId);
        menu.setSortInt(sortInt);
        menu.setParentName(parentName);
        menu.setRoute(route);
        menu.setIcon(icon);
        menu.setIsShow(isShow);
        menuService.updateMenu(menu);
        return responseResult;
    }


    /***
     * 删除菜单
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public ResponseResult deleteMenu(RequestParameter rp, @PathVariable("id") String id) {
        logger.info("菜单删除的入参==================【{}】===============", id);
        ResponseResult responseResult = new ResponseResult();
        menuService.deleteMenu(Integer.parseInt(id));
        return responseResult;
    }

    /***
     * 查询所有菜单
     * @param
     * @return
     */
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    @NotCheckIP
    @NotCheckLogin
    public ResponseResult getMenu() {
        ResponseResult responseResult = new ResponseResult();
        Map<String, Object> menus = menuService.selectMenus();
        responseResult.setMap(menus);
        return responseResult;
    }
}

